﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.IO;
using System.Text;
using System.Reflection;

using System.Web;


namespace GMS
{
    /// <summary>
    /// Summary description for Utility
    /// </summary>
    public class Utility
    {
        #region Constructor

        public Utility()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        #endregion Constructor

        #region Properties

        private static string ENCRYPTION_DESKEY
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["ENCRYPTION_DESKEY"];
            }
        }

        private static string ENCRYPTION_DESIV
        {
            get
            {
                return System.Configuration.ConfigurationManager.AppSettings["ENCRYPTION_DESIV"];
            }
        }

        #endregion Properties

        public static string DESDecrypt(string stringToDecrypt)
        {
            byte[] key;
            byte[] IV;
            byte[] inputByteArray;

            try
            {
                key = Convert2ByteArray(ENCRYPTION_DESKEY);
                IV = Convert2ByteArray(ENCRYPTION_DESIV);

                int len = stringToDecrypt.Length; inputByteArray = Convert.FromBase64String(stringToDecrypt);

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);

                cs.FlushFinalBlock();

                Encoding encoding = Encoding.UTF8; return encoding.GetString(ms.ToArray());
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public static string DESEncrypt(string stringToEncrypt)
        {
            byte[] key;
            byte[] IV;
            byte[] inputByteArray;

            try
            {
                key = Convert2ByteArray(ENCRYPTION_DESKEY);
                IV = Convert2ByteArray(ENCRYPTION_DESIV);

                inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);

                cs.FlushFinalBlock();

                return Convert.ToBase64String(ms.ToArray());
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        private static byte[] Convert2ByteArray(string strInput)
        {
            int intCounter; char[] arrChar;
            arrChar = strInput.ToCharArray();

            byte[] arrByte = new byte[arrChar.Length];

            for (intCounter = 0; intCounter <= arrByte.Length - 1; intCounter++)
            {
                arrByte[intCounter] = Convert.ToByte(arrChar[intCounter]);
            }

            return arrByte;
        }

        public static string GetEstimateEmailTemplate(string filePath)
        {
            StringBuilder sb = new StringBuilder();
            filePath = string.Concat(HttpContext.Current.Request.MapPath("..\\App_Data"), "\\", filePath);

            if (File.Exists(filePath))
            {
                StreamReader file = null;
                try
                {
                    file = new StreamReader(filePath);
                    string line;
                    while ((line = file.ReadLine()) != null)
                    {
                        sb.Append(line);
                        sb.Append("\n");
                    }
                }
                finally
                {
                    if (file != null)
                        file.Close();
                }
            }

            return sb.ToString();
        }

        public static double CalculateTotalCostWithOutTax(int backsplash, int backsplashPrice, int edge, int edgePrice, int sinkout,
            int sinkoutPrice, double surface, int surfacePrice, int zipDistance, int zipSurcharge)
        {
            zipSurcharge = zipSurcharge < 0 ? 0 : zipSurcharge;
            return (backsplash * backsplashPrice) + (edge * edgePrice) + (sinkout * sinkoutPrice) +
                (surface * surfacePrice) + (zipDistance * zipSurcharge);
        }

        public static double CalculateTotalCostWithTax(double total, double tax)
        {
            double returnValue = total + ((total * tax) / 100);
            return returnValue;
        }

        public static double CalculateTotalTax(int backsplash, int backsplashPrice, int edge, int edgePrice, int sinkout,
            int sinkoutPrice, double surface, int surfacePrice, int zipDistance, int zipSurcharge, double tax)
        {
            zipSurcharge = zipSurcharge < 0 ? 0 : zipSurcharge;
            double returnValue = (backsplash * backsplashPrice) + (edge * edgePrice) + (sinkout * sinkoutPrice) +
                (surface * surfacePrice) + (zipDistance * zipSurcharge);
            returnValue = (returnValue * tax) / 100;
            return returnValue;
        }

        // this function will convert inches to Foot 
        public static double CalculateShapeSizeIntoFT(int sizeInInches)
        {
            return Convert.ToDouble(sizeInInches / 12.0);
        }
        
    }
}